home *** CD-ROM | disk | FTP | other *** search
- /*
- reverse a sentence, eg `the cat sat on the mat'
-
- - reverse whole sentence to give `tam eht no tas tac eht'
-
- - reverse each word to give `mat the on sat cat the'
-
- all the reversing is in-place.
- */
-
-
- void main(), rev(char *l, char *r);
-
- #include <stdio.h>
-
- void main()
- {
- char buf[100], *end, *x, *y;
-
- gets(buf);
- for(end=buf; *end; end++) ;
- rev(buf,end-1); /* reverse sentence */
-
- x=buf-1; y=buf; /* now swap each word within sentence */
-
- while(x++<end)
- if(*x=='\0' || *x==' ')
- {
- rev(y,x-1);
- y=x+1;
- }
-
- printf("%s\n",buf);
- }
-
- void rev(char *l,char *r) /* reverse string */
- {
- char t;
-
- while(l<r) { t=*l; *l++=*r; *r--=t; }
- }
-